﻿using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml;

namespace RDPManager
{
    class RdpConfig
    {
        public string Name { get; private set; }
        // 目录
        public List<RdpConfig> Dirs = new List<RdpConfig>();
        // 单独的配置
        public List<SingleConfig> Singles = new List<SingleConfig>();

        public RdpConfig(string name)
        {
            Name = name;
        }

        public void parse(XmlElement root)
        {
            XmlNodeList xnl = root.ChildNodes;
            foreach (XmlNode child in xnl)
            {
                XmlElement elem = (XmlElement)child;
                if (elem.Name.Equals("dir"))
                {
                    string name = elem.GetAttribute("name");
                    RdpConfig rdpConfig = new RdpConfig(name);
                    rdpConfig.parse(elem);
                    Dirs.Add(rdpConfig);
                }
                else
                {
                    SingleConfig singleConfig = new SingleConfig();
                    singleConfig.parse(elem);
                    Singles.Add(singleConfig);
                }
            }
        }

        public void write(TreeNode node)
        {
            foreach (RdpConfig rdpConfig in Dirs)
            {
                TreeNode child = new TreeNode(rdpConfig.Name);
                rdpConfig.write(child);
                node.Nodes.Add(child);
            }
            foreach (SingleConfig singleConfig in Singles)
            {
                TreeNode child = new TreeNode(singleConfig.Name + "(" + singleConfig.IP + ")");
                child.Tag = singleConfig;
                node.Nodes.Add(child);
            }
        }
    }
}
